package com.feishi.core.util.http;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HttpUtil {
    public static void main(String[] args) throws Exception {
        HttpGet get= buildGet("https://www.baidu.com",null,null);
        CloseableHttpClient httpClient=getDefaultRequest();
        try {
            CloseableHttpResponse response = httpClient.execute(get);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                if(response.getEntity()==null){
                    return;
                }
                String result = EntityUtils.toString(response.getEntity());
                System.out.println(result);
            }

        } catch (Exception e) {
            throw e;
        }finally {
            httpClient.close();
        }
    }


    public static String get(String url) throws Exception {
        return get(url,null,null);
    }
    public static String get(String url, Map<String,String> params) throws Exception {
        return get(url,params,null);
    }
    private static List<NameValuePair> toNameValueList(Map<String, String> params) {
        // TODO Auto-generated method stub
        if(params==null||params.size()==0){
            return null;
        }
        List<NameValuePair> list=new ArrayList<>();
        for (Map.Entry<String, String> entry:params.entrySet()) {
            list.add(new NameValuePairBase(entry.getKey(), entry.getValue()));
        }
        return list;
    }

    public static String get(String url, Map<String,String> values, Map<String,String> header) throws Exception {
        return HttpClientPoll.sendRequest(buildGet(url,values,header));
    }
    public static String post(String url) throws Exception {
        return post(url,null,null,null);
    }
    public static String post(String url, Map<String,String> params) throws Exception {
        return post(url,params,null,null);
    }
    public static String post(String url, String content) throws Exception {
        return post(url,null,content,null);
    }

    public static String post(String url, Map<String,String> values, String content, Map<String,String> header) throws Exception {
//        return sendRequest(HttpClients.createDefault(),buildPost(url,values,content,header));
        return HttpClientPoll.sendRequest(buildPost(url,values,content,header));
    }


    private static CloseableHttpClient getDefaultRequest() {
        return HttpClients.createDefault();
    }



    public static RequestConfig getStandardRequestConfig() {
        return RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT).build();
    }


    public static HttpGet buildGet(String url, Map<String,String> param, Map<String,String> header) throws Exception {
        if(param!=null){
            String paramStr= URLEncodedUtils.format(toNameValueList(param),Consts.UTF_8);
            url+=url.contains("?")?"&":"?"+paramStr;
        }
        HttpGet httpGet=new HttpGet(url);
        if(header!=null){
            for (Map.Entry<String, String> entry : header.entrySet()) {
                httpGet.setHeader(entry.getKey(),entry.getValue());
            }
        }
        return httpGet;

    }

    public static String sendRequest(CloseableHttpClient httpClient, HttpRequestBase request) throws IOException {
        try {
            CloseableHttpResponse response = httpClient.execute(request);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                if(response.getEntity()==null){
                    return null;
                }
                String result = EntityUtils.toString(response.getEntity());
                return result;
            }

        } catch (Exception e) {
            throw e;
        }finally {
            httpClient.close();
        }
        return null;
    }

    public static HttpPost buildPost(String url, Map<String,String> values, String content, Map<String,String> header) throws Exception {
        HttpPost post=new HttpPost(url);
        if(header!=null){
            for (Map.Entry<String, String> entry : header.entrySet()) {
                post.setHeader(entry.getKey(),entry.getValue());
            }
        }
        if(values!=null){
            HttpEntity entity = new UrlEncodedFormEntity(toNameValueList(values), Consts.UTF_8);
            post.setEntity(entity);
        }
        if(content!=null){
            HttpEntity entity =new StringEntity(content,Consts.UTF_8);
            post.setEntity(entity);
        }
        return post;
    }
}
